package com.ysd.servlet;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.ysd.entity.User;
import com.ysd.impl.UserDaoImpl;
import com.ysd.utli.Result;



	public class MessageServlet extends HttpServlet {
		 protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
			//request.setCharacterEncoding("UTF-8");//璁剧疆璇锋眰鐨勭紪鐮佹牸寮忎负UTF-8
			String username = request.getParameter("username");//鎺ュ彈鍓嶅彴浼犳潵鐨勫弬鏁�
			String phone = request.getParameter("phone");
			String email = request.getParameter("email");
			String content = request.getParameter("content");
			
			User user = new User(username,phone,email,content);
	        response.setCharacterEncoding("UTF-8");
			PrintWriter out = response.getWriter();
			
			UserDaoImpl uDao = new UserDaoImpl();
		    int n =	uDao.addMessage(user);//寰楀埌鍙楀奖鍝嶈鏁�
		    if(n>0) {//成功
				out.print(Result.toClient(true, "留言成功",""));//{"success":"true","message":"留言成功","remark":""}
			}else {//失败
				out.print(Result.toClient(false, "留言失败","服务器异常"));//{"success":"false","message":"留言失败","remark":"服务器异常"}
			}
			out.flush();
			out.close();
			
			
		}
		
		
		protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
			// TODO Auto-generated method stub
			doGet(request, response);
		}

	}

